package cn.iocoder.yudao.module.reading.dal.mysql.audio;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.reading.controller.admin.audio.vo.AudioResourceListReqVO;
import cn.iocoder.yudao.module.reading.controller.admin.audio.vo.AudioResourcePageReqVO;
import cn.iocoder.yudao.module.reading.dal.dataobject.audio.AudioResourceDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 音频资源 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface AudioResourceMapper extends BaseMapperX<AudioResourceDO> {

    /**
     * 根据分类查询音频资源列表
     *
     * @param category 音频分类
     * @param language 语言类型（可选）
     * @param limit    限制数量
     * @return 音频资源列表
     */
    default List<AudioResourceDO> selectByCategory(String category, String language, Integer limit) {
        return selectList(new LambdaQueryWrapperX<AudioResourceDO>()
                .eq(AudioResourceDO::getCategory, category)
                .eqIfPresent(AudioResourceDO::getLanguage, language)
                .eq(AudioResourceDO::getEnabled, true)
                .orderByAsc(AudioResourceDO::getSortOrder)
                .orderByDesc(AudioResourceDO::getCreateTime)
                .last(limit != null ? "LIMIT " + limit : ""));
    }

    /**
     * 根据绘本ID查询音频资源
     *
     * @param bookId 绘本ID
     * @return 音频资源列表
     */
    default List<AudioResourceDO> selectByBookId(Long bookId) {
        return selectList(new LambdaQueryWrapperX<AudioResourceDO>()
                .eq(AudioResourceDO::getBookId, bookId)
                .eq(AudioResourceDO::getEnabled, true)
                .orderByAsc(AudioResourceDO::getSortOrder));
    }

    /**
     * 根据绘本集ID查询音频资源
     *
     * @param bookSetId 绘本集ID
     * @return 音频资源列表
     */
    default List<AudioResourceDO> selectByBookSetId(Long bookSetId) {
        return selectList(new LambdaQueryWrapperX<AudioResourceDO>()
                .eq(AudioResourceDO::getBookSetId, bookSetId)
                .eq(AudioResourceDO::getEnabled, true)
                .orderByAsc(AudioResourceDO::getSortOrder));
    }

    /**
     * 搜索音频资源
     *
     * @param keyword  关键词
     * @param language 语言类型（可选）
     * @return 音频资源列表
     */
    default List<AudioResourceDO> searchAudioResources(String keyword, String language) {
        return selectList(new LambdaQueryWrapperX<AudioResourceDO>()
                .likeIfPresent(AudioResourceDO::getTitle, keyword)
                .eqIfPresent(AudioResourceDO::getLanguage, language)
                .eq(AudioResourceDO::getEnabled, true)
                .orderByDesc(AudioResourceDO::getCreateTime));
    }

    /**
     * 获取推荐音频资源
     *
     * @param language 语言类型（可选）
     * @param limit    限制数量
     * @return 音频资源列表
     */
    default List<AudioResourceDO> selectRecommended(String language, Integer limit) {
        return selectList(new LambdaQueryWrapperX<AudioResourceDO>()
                .eqIfPresent(AudioResourceDO::getLanguage, language)
                .eq(AudioResourceDO::getEnabled, true)
                .orderByAsc(AudioResourceDO::getSortOrder)
                .orderByDesc(AudioResourceDO::getCreateTime)
                .last(limit != null ? "LIMIT " + limit : ""));
    }

    /**
     * 获取最大ID
     *
     * @return 最大ID，如果表为空则返回null
     */
    @Select("SELECT MAX(id) FROM audio_resources")
    Long selectMaxId();

    // ==================== 管理后台方法 ====================

    /**
     * 分页查询音频资源
     *
     * @param pageReqVO 分页查询条件
     * @return 分页结果
     */
    default PageResult<AudioResourceDO> selectPage(AudioResourcePageReqVO pageReqVO) {
        return selectPage(pageReqVO, new LambdaQueryWrapperX<AudioResourceDO>()
                .likeIfPresent(AudioResourceDO::getTitle, pageReqVO.getTitle())
                .eqIfPresent(AudioResourceDO::getCategory, pageReqVO.getCategory())
                .eqIfPresent(AudioResourceDO::getLanguage, pageReqVO.getLanguage())
                .eqIfPresent(AudioResourceDO::getBookId, pageReqVO.getBookId())
                .eqIfPresent(AudioResourceDO::getBookSetId, pageReqVO.getBookSetId())
                .eqIfPresent(AudioResourceDO::getEnabled, pageReqVO.getEnabled())
                .orderByAsc(AudioResourceDO::getSortOrder)
                .orderByDesc(AudioResourceDO::getCreateTime));
    }

    /**
     * 列表查询音频资源
     *
     * @param listReqVO 列表查询条件
     * @return 音频资源列表
     */
    default List<AudioResourceDO> selectList(AudioResourceListReqVO listReqVO) {
        return selectList(new LambdaQueryWrapperX<AudioResourceDO>()
                .likeIfPresent(AudioResourceDO::getTitle, listReqVO.getTitle())
                .eqIfPresent(AudioResourceDO::getCategory, listReqVO.getCategory())
                .eqIfPresent(AudioResourceDO::getLanguage, listReqVO.getLanguage())
                .eqIfPresent(AudioResourceDO::getBookId, listReqVO.getBookId())
                .eqIfPresent(AudioResourceDO::getBookSetId, listReqVO.getBookSetId())
                .eqIfPresent(AudioResourceDO::getEnabled, listReqVO.getEnabled())
                .orderByAsc(AudioResourceDO::getSortOrder)
                .orderByDesc(AudioResourceDO::getCreateTime));
    }

    /**
     * 批量更新音频资源状态
     *
     * @param ids     音频资源ID列表
     * @param enabled 是否启用
     */
    @Update("<script>" +
            "UPDATE audio_resources SET enabled = #{enabled} WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    void updateBatchStatus(@Param("ids") List<Long> ids, @Param("enabled") Boolean enabled);

}
